package com.changgou.order.mq.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Map;

@Component
///** 监听的队列 */
@RabbitListener(queues = "${mq.pay.queue.order}")
public class OrderMessageListener {
  @Autowired
  private OrderService orderService;
  /** 支付的结果的监听 */
  @RabbitHandler
  public void getMessage(String message) throws ParseException {
    System.out.println("监听到的消息是" + message);
    // 支付的结果
    Map<String, String> map = JSON.parseObject(message, Map.class);
    System.out.println(map);
    // 通信标识
    String return_code = map.get("return_code");
    if (return_code.equals("SUCCESS")) {
      // 业务结果
      String result_code = map.get("result_code");
      // 订单号
      String outTradeNo = map.get("out_trade_no");

      if (result_code.equals("SUCCESS")) {
        // 进行订单的修改
        orderService.updateStatus(outTradeNo,"20191030133525","12356454575");
      }else {
        // 支付失败   调用微信的关闭订单 关闭订单  库存回滚
      }
    }
  }
}
